package com.yxwl.demo.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.yxwl.demo.R;
import com.yxwl.demo.utils.OnItemClickListner;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    public Context context;
    public LayoutInflater inflater;
    public List<T> datas = new ArrayList<>();
    public RecyclerView.LayoutManager layoutManager;

    public BaseRecyclerViewAdapter(Context context) {
        this(context, null);
    }

    public BaseRecyclerViewAdapter(Context context, List<T> datas) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.datas.clear();
        if (datas != null) {
            this.datas.addAll(datas);
        }
    }



    public void setDatas(List<T> datas) {
        this.datas.clear();
        if (datas != null) {
            this.datas.addAll(datas);
        }
        notifyDataSetChanged();
    }




    public void addData(T data) {
        int index = this.datas.size();
        this.datas.add(data);
        notify(index, 1);
    }
    public void notify(int size, int count) {
        notifyItemInserted(size);
        notifyItemRangeChanged(size, count);
    }


    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BaseViewHolder(getInflate(parent, viewType));
    }



    protected View getInflate(@NonNull ViewGroup parent, int viewType) {
        return inflater.inflate(getLayoutId(parent, viewType), parent, false);
    }

    protected @LayoutRes
    int getLayoutId(ViewGroup parent, int position) {
        return 0;
    }


    protected void bindData(BaseViewHolder holder, T data, int position) {
    }



    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position, @NonNull List<Object> payloads) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads);
        } else {
            notifyItem(holder, position, payloads);
        }
    }

    public void notifyItem(BaseViewHolder holder, int position, List<Object> payloads) {

    }


    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        bindData(holder, getItem(position), position);
    }

    public T getItem(int position) {
        int i = position;
        if (i < 0 || datas == null || datas.isEmpty() || i >= datas.size()) {
            return null;
        }
        return datas.get(i);
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }



    protected OnItemClickListner onItemClickListner;

    public void setOnItemClickListner(OnItemClickListner onItemClickListner) {
        this.onItemClickListner = onItemClickListner;
    }

    private View.OnClickListener listener;

    protected View.OnClickListener getListener() {
        if (listener == null) {
            listener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callListen(0, v);
                }

            };
        }
        return listener;
    }





    private boolean callListen(int clickType, View v) {
        int type = 0;
        int postion = 0;
        Object typeTag = v.getTag(clickType == 0 ? R.id.item_type : R.id.item_type_long);
        if (typeTag != null) {
            type = (int) typeTag;
        }
        Object postionTtag = v.getTag(clickType == 0 ? R.id.item_postion : R.id.item_postion_long);
        if (postionTtag != null) {
            postion = (int) postionTtag;
        }
        Object obj = v.getTag(clickType == 0 ? R.id.item_obj : R.id.item_obj_long);

        if (call(v, type, obj, postion)) {
            return true;
        }
        if (onItemClickListner != null) {
            return onItemClickListner.onItemClickListner(v, type, postion, obj);
        }
        return false;
    }

    protected boolean call(View v, int type, Object t, int position) {
        return false;
    }


    protected void setViewClick(View v, int type, int postion, Object obj) {
        initTag(0, v, type, postion, obj);
        v.setOnClickListener(getListener());
    }



    protected void initTag(int clickType, View v, int type, int postion, Object obj) {
        if (clickType == 0) {
            v.setTag(R.id.item_type, type);
            v.setTag(R.id.item_postion, postion);
            v.setTag(R.id.item_obj, obj);
        } else {
            v.setTag(R.id.item_type_long, type);
            v.setTag(R.id.item_postion_long, postion);
            v.setTag(R.id.item_obj_long, obj);
        }
    }
}

